//
//  ForumViewController.m
//  Project_A Gary
//
//  Created by leilei on 16/3/23.
//  Copyright © 2016年 www.lei.com. All rights reserved.
//

#import "ForumViewController.h"
#import "ForumModel.h"
#import "ForumTableViewCell.h"
#import "ForumDetailViewController.h"
#import "TitleView.h"
#import "rightTableViewCell.h"
#import "rightModel.h"
#import "rightSecondTableViewCell.h"

@interface ForumViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableV;
@property (nonatomic, retain)UIView *clickView;

@property (nonatomic, retain)NSMutableArray *tableArr;

// 右边综合论坛的数组
@property (nonatomic, retain)NSMutableArray *rightArr;
@property (nonatomic, retain)NSMutableArray *authorRightArr;
// 右边tableView
@property (nonatomic, retain)UITableView *rightTableV;


@property (nonatomic, retain)UISegmentedControl *seg;

@end

@implementation ForumViewController
-(void)dealloc
{
    [_tableV release];
    [_clickView release];
    [_tableArr release];
    [_rightArr release];
    [_authorRightArr release];
    [_rightTableV release];
    [_seg release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor purpleColor];
    
    // 给导航控制器填色
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:32 / 255.0 green:105 / 255.0 blue:236 / 255.0 alpha:1];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    // 创建分段控制器
    [self createSegmented];
    
    // 创建左边tableView
    [self createTableV];
    // tableView的数据解析
    [self tableVIewDataHandle];
    
    
    // 创建右边tableView
    [self createRightTableVIew];
    [self rightTableViewDataHandle];
}

#pragma mark - 右边tableView解析数据
- (void)rightTableViewDataHandle
{
    NSString *str = @"http://mrobot.pcauto.com.cn/v3/bbs/newForumsv45/18205?pageNo=1&pageSize=20&orderby=replyat&needImage=true&timestamp=1459213934038";
    [NetworkHandle GetDataWithURL:str dic:nil success:^(id responseObject) {
        
        // 标题的数组
        self.rightArr = responseObject[@"topicList"];
        
        
        [_rightTableV reloadData];
    } filed:^(NSError *error) {
        
    }];
}

#pragma mark - 创建右边tableView
- (void)createRightTableVIew
{
    self.rightTableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, BOUNDS_WIDTH, BOUNDS_HEIGHT) style:UITableViewStylePlain];
    self.rightTableV.backgroundColor = [UIColor whiteColor];
    
    self.rightTableV.delegate = self;
    self.rightTableV.dataSource = self;
//    [self.view addSubview:self.rightTableV];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.rightTableV.rowHeight = 100;
    [_rightTableV release];
    
    [self.rightTableV registerClass:[rightTableViewCell class] forCellReuseIdentifier:@"rightTableViewCell"];
    
    // 带图片论坛的tableViewCell
    [self.rightTableV registerClass:[rightSecondTableViewCell class] forCellReuseIdentifier:@"rightSecondTableViewCell"];
}




#pragma mark - 左边tableView解析数据
- (void)tableVIewDataHandle
{
    NSString *str = @"http://mrobot.pcauto.com.cn/xsp/s/auto/info/xueChe/communityHomePage.xsp?pageNo=1&smallImg=true&bbsImgOnly=true&type=json&picRule=1&imgCount=3&visitedFid=17613D21085D20243";
    [NetworkHandle GetDataWithURL:str dic:nil success:^(id responseObject) {
        
        self.tableArr = [ForumModel transformWithArray:responseObject[@"data"]];
        [self.tableV reloadData];
        
    } filed:^(NSError *error) {
        
    }];
}



#pragma mark - 创建分页控制器
- (void)createSegmented
{
    
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"论坛广场", @"综合论坛"]];
    self.seg.frame = CGRectMake(0, 0, 180, 30);
    self.navigationItem.titleView = self.seg;
    self.seg.backgroundColor = [UIColor clearColor];
    [_seg release];
    //首先选中的下角标
    self.seg.selectedSegmentIndex = 0;
    // 渲染的颜色
    self.seg.tintColor = [UIColor whiteColor];
    // 添加点击方法
    [self.seg addTarget:self action:@selector(segmentedClick:) forControlEvents:UIControlEventValueChanged];
    
}
- (void)segmentedClick: (UISegmentedControl *)seg
{
    if (0 == seg.selectedSegmentIndex) {
        [UIView transitionFromView:self.rightTableV toView:self.tableV duration:1 options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished) {
            
        }];
        //普通效果 没有动画切换界面
//        [self.view bringSubviewToFront:self.tableV]
        ;
    } else if(1 == seg.selectedSegmentIndex)  {
        [UIView transitionFromView:self.tableV toView:self.rightTableV duration:1 options:UIViewAnimationOptionTransitionFlipFromRight completion:^(BOOL finished) {
            
        }];
        // 普通效果没有动画效果的切换界面
//        [self.view  bringSubviewToFront:self.clickView];
    }
}












#pragma mark - 创建左边tableV
- (void)createTableV
{
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, BOUNDS_WIDTH, BOUNDS_HEIGHT) style:UITableViewStylePlain];
    self.tableV.delegate = self;
    self.tableV.dataSource  =self;
    self.tableV.rowHeight = 225;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.tableV];
    self.tableV.backgroundColor = [UIColor whiteColor];
    [_tableV release];
    [self.tableV registerClass:[ForumTableViewCell class] forCellReuseIdentifier:@"ForumTableViewCell"];
    
}

#pragma mark - 创建tableView的协会方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (0 == self.seg.selectedSegmentIndex) {
        return self.tableArr.count;
    } else {
        return self.rightArr.count;
    }
}

// cell 判断用哪个tableView时候不能下角标来进行判断
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableV) {
        ForumTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ForumTableViewCell" forIndexPath:indexPath];
        ForumModel *FModel = self.tableArr[indexPath.row];
        cell.titleLable.text = FModel.title;
        cell.summaryLable.text = FModel.summary;
        [cell.picImg sd_setImageWithURL:[NSURL URLWithString:FModel.pic]];
        
        
        cell.backgroundColor = [UIColor colorWithRed:236 / 255.0 green:236 / 255.0 blue:236 / 255.0 alpha:1];
        
        return cell;

    }else{
    
        if (0 == [self.rightArr[indexPath.row][@"images"] count]) {
            rightTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rightTableViewCell"];
            cell.title.text = self.rightArr[indexPath.row][@"title"];
            cell.nickname.text = self.rightArr[indexPath.row][@"author"][@"nickname"];
            [cell.userface sd_setImageWithURL:[NSURL URLWithString:self.rightArr[indexPath.row][@"author"][@"userface"]]];
            return cell;
        }else if (1 == [self.rightArr[indexPath.row][@"images"] count]){
            rightSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rightSecondTableViewCell" forIndexPath:indexPath];
            cell.title.text = self.rightArr[indexPath.row][@"title"];
            cell.nickname.text = self.rightArr[indexPath.row][@"author"][@"nickname"];
            [cell.userface sd_setImageWithURL:[NSURL URLWithString:self.rightArr[indexPath.row][@"author"][@"userface"]]];
            [cell.picOne sd_setImageWithURL:[NSURL URLWithString:self.rightArr[indexPath.row][@"images"][0][@"url"]]];
            return cell;
            
        }else if (2 == [self.rightArr[indexPath.row][@"images"] count]){
            rightSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rightSecondTableViewCell" forIndexPath:indexPath];
            cell.title.text = self.rightArr[indexPath.row][@"title"];
            cell.nickname.text = self.rightArr[indexPath.row][@"author"][@"nickname"];
            [cell.userface sd_setImageWithURL:[NSURL URLWithString:self.rightArr[indexPath.row][@"author"][@"userface"]]];
            [cell.picOne sd_setImageWithURL:[NSURL URLWithString:self.rightArr[indexPath.row][@"images"][0][@"url"]]];
            [cell.picTwo sd_setImageWithURL:[NSURL URLWithString:self.rightArr[indexPath.row][@"images"][1][@"url"]]];
            return cell;

        }else {
            rightSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rightSecondTableViewCell" forIndexPath:indexPath];
            cell.title.text = self.rightArr[indexPath.row][@"title"];
            cell.nickname.text = self.rightArr[indexPath.row][@"author"][@"nickname"];
            [cell.userface sd_setImageWithURL:[NSURL URLWithString:self.rightArr[indexPath.row][@"author"][@"userface"]]];
            [cell.picOne sd_setImageWithURL:[NSURL URLWithString:self.rightArr[indexPath.row][@"images"][0][@"url"]]];
            [cell.picTwo sd_setImageWithURL:[NSURL URLWithString:self.rightArr[indexPath.row][@"images"][1][@"url"]]];
            [cell.picThree sd_setImageWithURL:[NSURL URLWithString:self.rightArr[indexPath.row][@"images"][2][@"url"]]];
            
            //cell.imageArray = self.rightArr[indexPath.row][@"images"];
            
            return cell;
            

            }
    
            
        }
}


#pragma mark - tableView的高度方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (0 == self.seg.selectedSegmentIndex) {
        return 225;
    }else {
        if (0 == [self.rightArr[indexPath.row][@"images"] count]) {
            return 100;
        } else {
            return 200;
        }

    }
    
    
}

#pragma mark - tableView的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ForumDetailViewController *forumDetailVC = [[ForumDetailViewController alloc] init];
    ForumModel *fModel = self.tableArr[indexPath.row];
    // 传递网址的数据
    forumDetailVC.url = fModel.url;
    // 在跳转详情页面的时候会隐藏tabbar
    [forumDetailVC setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:forumDetailVC animated:YES];
    
    [forumDetailVC release];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
